package com.example.demo.controller;


import com.example.demo.common.AjaxResult;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.*;
import com.example.demo.entity.vo.CheckArticleinfoVO;
import com.example.demo.entity.vo.DelArticleinfoVO;
import com.example.demo.service.ArticleService;
import com.example.demo.util.RemoveMarkdownTag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private ArticleService articleService;


    /**
     * 管理员删除文章
     */
    @RequestMapping("/del")
    public AjaxResult del(Integer id){

        if (id == null || id <= 0){
            //参数有误
            return AjaxResult.fail(-1,"参数异常");
        }

        //id表示文章id，通过这个id找出来文章的信息，放到删除文章的类中，再存入delArticle表中
        Articleinfo articleinfo = articleService.getArticleById(id);

        DelArticleinfo delArticleinfo = new DelArticleinfo();
        delArticleinfo.setAid(id);
        delArticleinfo.setUid(articleinfo.getUid());
        delArticleinfo.setTitle(articleinfo.getTitle());
        delArticleinfo.setDelTime(LocalDateTime.now());

        //将删除的文章信息存到delarticle 表中
        articleService.saveDelArticle(delArticleinfo);

        //修改这个要删除文章的id为3
        return AjaxResult.success(articleService.updateState(id,3));
    }


    /**
     *
     * 找自己的文章是否被管理员删除删除
     *
     * */
    @RequestMapping("/articleByDel")
    public AjaxResult selectDelArticle(HttpServletRequest request){
        List<DelArticleinfo> list = new ArrayList<>();
        List<DelArticleinfoVO> list1 = new ArrayList<>();


        Userinfo user = UserSessionUtils.getSessionUser(request);
        int uid = user.getId();

        list = articleService.getAllDelArticleByUId(uid);
        //将List中的每个对象中的属性换成他的子类

        for(DelArticleinfo x : list){
            DelArticleinfoVO delArticleInfoVO = new DelArticleinfoVO();
            delArticleInfoVO.setAid(x.getAid());
            delArticleInfoVO.setUid(x.getUid());
            delArticleInfoVO.setTitle(x.getTitle());
            delArticleInfoVO.setDelTime(x.getDelTime());
            delArticleInfoVO.setState(x.getState());
            delArticleInfoVO.setDescripe("您的文章名为\""+x.getTitle()+"\"的文章被管理员删除");
            delArticleInfoVO.setSuperTable("delArticle");
            list1.add(delArticleInfoVO);
        }

        //调用一个方法，在checkArticle表中查找管理员审核文章的信息，state为1，表示审核通过，state为2表示审核不通过
        //两种状态都要查出来    需要根据当前登录的用户id查找他的通知

        List<CheckArticleinfo> checkList = articleService.getAllCheckArticleInCheckArticle(uid);
        for(CheckArticleinfo x : checkList){
            DelArticleinfoVO delArticleInfoVO = new DelArticleinfoVO();
            delArticleInfoVO.setAid(x.getAid());
            delArticleInfoVO.setUid(x.getUid());
            delArticleInfoVO.setTitle(x.getTitle());
            delArticleInfoVO.setDelTime(x.getDelTime());
            delArticleInfoVO.setState(x.getState());
            if (x.getState()==1){
                delArticleInfoVO.setDescripe("您的文章名为\""+x.getTitle()+"\"的文章已经审核通过");
            }else if(x.getState()==2){
                delArticleInfoVO.setDescripe("您的文章名为\""+x.getTitle()+"\"的文章没有被审核通过");
            }
            delArticleInfoVO.setSuperTable("checkArticle");
            list1.add(delArticleInfoVO);
        }

        //将list1中的所有对象根据修改时间进行排序
       list1.sort(new Comparator<DelArticleinfoVO>() {
           @Override
           public int compare(DelArticleinfoVO o1, DelArticleinfoVO o2) {
               return o2.getDelTime().compareTo(o1.getDelTime());
           }
       });


        return AjaxResult.success(list1);



    }


    //查询被删除的历史通知
    @RequestMapping("/historyNoti")
    public AjaxResult historyNoti(HttpServletRequest request){
        List<HistoryNotiinfo> list = new ArrayList<>();
        Userinfo user = UserSessionUtils.getSessionUser(request);
        int uid = user.getId();
        //在historyNoti表中找被删除的通知，根据uid找
        list = articleService.getHistoryNoti(uid);

        return AjaxResult.success(list);

    }


    //查询待审核的所有文章     state为2的代表待审核的文章    state为3代表被管理员删除的文章
    @RequestMapping("/getAllCheckArticle")
    public AjaxResult getAllCheckArticle(HttpServletRequest request){

        //查看管理员是否已经登录
        Userinfo user = UserSessionUtils.getSessionUser(request);
        if (user == null || user.getId()<=0){
            return AjaxResult.fail(0,"参数非法",-1);
        }

        List<Articleinfo> list = new ArrayList<>();
        //获取所有state为2的文章
        list = articleService.getAllCheckArticle();

        //在这里去除markdown的标签
        RemoveMarkdownTag removeMarkdownTag = new RemoveMarkdownTag();
        for(Articleinfo articleinfo:list){
            //在这里去除markdown的标签
            String text = articleinfo.getContent();
            articleinfo.setContent(removeMarkdownTag.removeMarkdownTags(text));
            //判断字符的数量，超过200字省略后边的东西
            if (articleinfo.getContent().length()>=200){
                String tmp = articleinfo.getContent().substring(0,200)+". . .";
                articleinfo.setContent(tmp);
            }
        }


        return AjaxResult.success(list);

    }


    //审核通过文章
    @RequestMapping("/pass")
    public AjaxResult pass(Integer id,HttpServletRequest request){
        int aid = id;
        //根据aid就直接可以更新，因为位置id唯一
        articleService.updateState(aid,1);
        //查询出这个文章的信息，然后插入到checkArticle表中
        Articleinfo articleinfo = articleService.getArticleById(aid);
        //将这个信息存到checkArticle表中
        CheckArticleinfo checkArticleinfo = new CheckArticleinfo();
        checkArticleinfo.setAid(aid);
        checkArticleinfo.setUid(articleinfo.getUid());
        checkArticleinfo.setTitle(articleinfo.getTitle());
        checkArticleinfo.setDelTime(LocalDateTime.now());
        checkArticleinfo.setState(1);


        return AjaxResult.success(articleService.insertCheckArticle(checkArticleinfo));


    }

    //审核不通过文章
    @RequestMapping("/nopass")
    public AjaxResult nopass(Integer id,HttpServletRequest request){
        int aid = id;
        //根据aid就直接可以更新，因为位置id唯一
        articleService.updateState(aid,4);
        //查询出这个文章的信息，然后插入到checkArticle表中
        Articleinfo articleinfo = articleService.getArticleById(aid);
        //将这个信息存到checkArticle表中
        CheckArticleinfo checkArticleinfo = new CheckArticleinfo();
        checkArticleinfo.setAid(aid);
        checkArticleinfo.setUid(articleinfo.getUid());
        checkArticleinfo.setTitle(articleinfo.getTitle());
        checkArticleinfo.setDelTime(LocalDateTime.now());
        checkArticleinfo.setState(2);

        return AjaxResult.success(articleService.insertCheckArticle(checkArticleinfo));


    }






}















